package com.cloud.sample.controller;

import com.alibaba.excel.EasyExcel;
import com.cloud.sample.bean.StudentInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpHeaders;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * 导出接口
 */
@Slf4j
@Controller
public class ExportController {
    /**
     * 导出学生信息
     *
     * @param response
     * @throws IOException
     */
    @GetMapping("/export/student")
    public void exportStudent(HttpServletResponse response) throws IOException {
        response.setContentType("application/msexcel; charset=UTF-8");
        response.setHeader(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=student.xlsx");

        List<StudentInfo> list = new ArrayList<>();
        EasyExcel.write(response.getOutputStream(), StudentInfo.class).sheet().doWrite(list);
    }
}
